library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
#read our cleaned up data into the env
data_analytic<- readRDS("../../../../test/data/interim/analytic2023_c1.rds")
#we want to plot poor mental health days and PHS
#race break down DNE
#PMHD(v042_rawvalue):Average number of mentally unhealthy days reported in past 30 days (age-adjusted).
ggplot(data_analytic, aes(y = v005_rawvalue, x = v042_rawvalue))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab(paste("Average number of mentally unhealthy days reported in past 30 days (age-adjusted).")) +
  ylab("Preventable Hospital Stays")+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 71 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 71 rows containing missing values (`geom_point()`).

#we want to plot poor mental health days and PHS by race
#PMHD(v042_rawvalue):Average number of mentally unhealthy days reported in past 30 days (age-adjusted).
v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
for( var in v005_race_yaxis){
v005byraceVsPMHD <- ggplot(data_analytic, aes_string(y = var, x = "v042_rawvalue"))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab("Average number of mentally unhealthy days reported in past 30 days (age-adjusted).") +
  ylab(paste(v005_race_yaxis,":Preventable Hospital Stays"))+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
  print(v005byraceVsPMHD)
}
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2222 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2222 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 105 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 105 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2760 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2760 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2809 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2809 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1765 rows containing missing values (`geom_point()`).

#phs vs frequent mental distress
#race break down DNE
#FMD(v145):Percentage of adults reporting 14 or more days of poor mental health per month (age-adjusted).
ggplot(data_analytic, aes(y = v005_rawvalue, x = v145_rawvalue))+
  geom_point(color="green",alpha =0.5)+
  geom_smooth() +
  xlab(paste("Ratio of household income at the 80th percentile to income at the 20th percentile.")) +
  ylab("Preventable Hospital Stays") +
  theme(panel.background = element_rect(fill = "black"),
        plot.background = element_rect(fill = "black"),
        panel.grid.major = element_line(color = "white", size = 0.1),
        panel.grid.minor = element_line(color = "white", size = 0.05),
        text = element_text(color = "white"))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 71 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 71 rows containing missing values (`geom_point()`).

v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
for( var in v005_race_yaxis){
v145byraceVsPMHD <- ggplot(data_analytic, aes_string(y = var, x = "v145_rawvalue"))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab("Average number of mentally unhealthy days reported in past 30 days (age-adjusted).") +
  ylab(paste(v005_race_yaxis,":Preventable Hospital Stays"))+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
  print(v005byraceVsPMHD)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

#phs vs poor or fair health 
#race break down DNE
#PFH(v002):Percentage of adults reporting fair or poor health (age-adjusted).
ggplot(data_analytic, aes(y = v005_rawvalue, x = v002_rawvalue))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab(paste("Percentage of adults reporting fair or poor health (age-adjusted)")) +
  ylab("Preventable Hospital Stays") +
  theme(panel.background = element_rect(fill = "black"),
        plot.background = element_rect(fill = "black"),
        panel.grid.major = element_line(color = "white", size = 0.1),
        panel.grid.minor = element_line(color = "white", size = 0.05),
        text = element_text(color = "white"))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 71 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 71 rows containing missing values (`geom_point()`).

v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
for( var in v005_race_yaxis){
v002byraceVsPMHD <- ggplot(data_analytic, aes_string(y = var, x = "v002_rawvalue"))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab("Average number of mentally unhealthy days reported in past 30 days (age-adjusted).") +
  ylab(paste(v005_race_yaxis,":Preventable Hospital Stays"))+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
  print(v005byraceVsPMHD)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Removed 1765 rows containing missing values (`geom_point()`).

#phs vs Drug overdase Raw
#v138 Drug overdose: Number of drug poisoning deaths per 100,000 population.
ggplot(data_analytic, aes(y = v005_rawvalue, x = v138_rawvalue))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab(paste("Number of drug poisoning deaths per 100,000 population.")) +
  ylab("Preventable Hospital Stays") +
  theme(panel.background = element_rect(fill = "black"),
        plot.background = element_rect(fill = "black"),
        panel.grid.major = element_line(color = "white", size = 0.1),
        panel.grid.minor = element_line(color = "white", size = 0.05),
        text = element_text(color = "white"))
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1347 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1347 rows containing missing values (`geom_point()`).

#phs vs Drug overdase by race
v138_xaxis_vars <- c("v138_race_hispanic", "v138_race_white", "v138_race_asian", "v138_race_aian", "v138_race_black")
for(var in v138_xaxis_vars){
  va138byracegraph <- ggplot(data_analytic, aes_string(y = "v005_rawvalue", x = var))+
    geom_point(color="green", alpha =0.5)+
    geom_smooth()+
    xlab(paste(var, ":Number of drug poisoning deaths per 100,000 population.")) +
    ylab("Preventable Hospital Stays")+
    theme(panel.background = element_rect(fill = "black"),
          plot.background = element_rect(fill = "black"),
          panel.grid.major = element_line(color = "white", size = 0.1),
          panel.grid.minor = element_line(color = "white", size = 0.05),
          text = element_text(color = "white")) 
    print(va138byracegraph)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2859 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2859 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1461 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1461 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 3103 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 3103 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 3102 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 3102 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2745 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2745 rows containing missing values (`geom_point()`).

v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
for( var in v005_race_yaxis){
v005byraceVsPMHD <- ggplot(data_analytic, aes_string(y = var, x = "v042_rawvalue"))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth()+
  xlab("Average number of mentally unhealthy days reported in past 30 days (age-adjusted).") +
  ylab(paste(var,":Preventable Hospital Stays"))+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
  print(v005byraceVsPMHD)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2222 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2222 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 105 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 105 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2760 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2760 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2809 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2809 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1765 rows containing missing values (`geom_point()`).

v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
for( var in v005_race_yaxis){
v005byraceVsPMHD <- ggplot(data_analytic, aes_string(y = paste0("log(",var,")"), x = "(v042_rawvalue)"))+
  geom_point(color="green", alpha =0.5)+
  geom_smooth() + 
  xlab("Average log number of mentally unhealthy days reported in past 30 days (age-adjusted).") +
  ylab(paste(var,": log Preventable Hospital Stays"))+
  theme(panel.background = element_rect(fill = "black"),
           plot.background = element_rect(fill = "black"),
           panel.grid.major = element_line(color = "white", size = 0.1),
           panel.grid.minor = element_line(color = "white", size = 0.05),
           text = element_text(color = "white"))
  print(v005byraceVsPMHD)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2222 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2222 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 105 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 105 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2760 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2760 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2809 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2809 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1765 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1765 rows containing missing values (`geom_point()`).

v005_race_yaxis <- c("v005_race_hispanic","v005_race_white","v005_race_asian","v005_race_aian","v005_race_black")
v044_race_xaxis <- c("v063_race_hispanic", "v063_race_white", "v063_race_asian", "v063_race_aian", "v063_race_black")

for(i in 1:length(v044_race_xaxis)){
  var_x <- v044_race_xaxis[i]
  var_y <- v005_race_yaxis[i]
  
  v044byracegraph <-ggplot(data_analytic, aes_string(y = paste0("log(",var_y, ")"), x = paste0("log(",var_x,")")))+
    geom_point(color="green", alpha =0.5)+
    geom_smooth()+
    xlab(paste(var_x, ":income median.")) +
    ylab(paste(var_y, ":Preventable Hospital Stays"))+
    theme(panel.background = element_rect(fill = "black"),
          plot.background = element_rect(fill = "black"),
          panel.grid.major = element_line(color = "white", size = 0.1),
          panel.grid.minor = element_line(color = "white", size = 0.05),
          text = element_text(color = "white")) 
  print(v044byracegraph)
}
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2230 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2230 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 106 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 106 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2769 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2769 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2831 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 2831 rows containing missing values (`geom_point()`).

## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 1835 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1835 rows containing missing values (`geom_point()`).